package com.zp.business.controller.prescription;


import com.zp.business.api.prescription.bo.PrescriptionItemBo;
import com.zp.business.api.prescription.vo.PrescriptionItemVo;
import com.zp.business.service.prescription.IPrescriptionItemService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 处方子prescription_item控制器
 * 前端访问路由地址为:/business/prescriptionItem
 *
 * @author zhang peng
 * @ date 2024-03-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/prescriptionItem")
public class PrescriptionItemController extends BaseController {

    private final IPrescriptionItemService iPrescriptionItemService;

    /**
     * 查询处方子prescription_item列表 (分页)
     */
//    @SaCheckPermission("business:prescriptionItem:list")
    @GetMapping("/selectPage")
    public TableDataInfo<PrescriptionItemVo> selectPage(PrescriptionItemBo bo, PageQuery pageQuery) {
        return iPrescriptionItemService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询处方子prescription_item列表 (不分页)
     */
//    @SaCheckPermission("business:prescriptionItem:list")
    @GetMapping("/list")
    public R<List<PrescriptionItemVo>> list(PrescriptionItemBo bo) {
        List<PrescriptionItemVo> list = iPrescriptionItemService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 查询子列表map
     */
    @PostMapping("/listPrescriptionItemMap")
    public R<Map<String, Object>> listPrescriptionItemMap(@RequestBody PrescriptionItemBo bo) {
        Map<String, Object> map = iPrescriptionItemService.listPrescriptionItemMap(bo);
        return R.ok(map);
    }

    /**
     * 确认发放
     */
    @PostMapping("/grantInfoOk")
    public R<Void> grantInfoOk(@RequestBody PrescriptionItemBo bo) {
        return toAjax(iPrescriptionItemService.grantInfoOk(bo));
    }

    /**
     * 确认退药
     */
    @PostMapping("/issuedGrantInfoOk")
    public R<Void> issuedGrantInfoOk(@RequestBody PrescriptionItemBo bo) {
        return toAjax(iPrescriptionItemService.issuedGrantInfoOk(bo));
    }

    /**
     * 导出处方子prescription_item列表
     */
//    @SaCheckPermission("business:prescriptionItem:export")
    @Log(title = "处方子prescription_item", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PrescriptionItemBo bo, HttpServletResponse response) {
        List<PrescriptionItemVo> list = iPrescriptionItemService.queryList(bo);
        ExcelUtil.exportExcel(list, "处方子prescription_item", PrescriptionItemVo.class, response);
    }

    /**
     * 获取处方子prescription_item详细信息
     *
     * @param itemId 主键
     */
//    @SaCheckPermission("business:prescriptionItem:list")
    @GetMapping("/{itemId}")
    public R<PrescriptionItemVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String itemId) {
        return R.ok(iPrescriptionItemService.queryById(itemId));
    }

    /**
     * 新增处方子prescription_item
     */
//    @SaCheckPermission("business:prescriptionItem:add")
    @Log(title = "处方子prescription_item", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PrescriptionItemBo bo) {
        return toAjax(iPrescriptionItemService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改处方子prescription_item
     */
//    @SaCheckPermission("business:prescriptionItem:edit")
    @Log(title = "处方子prescription_item", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PrescriptionItemBo bo) {
        return toAjax(iPrescriptionItemService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除处方子prescription_item
     *
     * @param itemIds 主键串
     */
//    @SaCheckPermission("business:prescriptionItem:remove")
    @Log(title = "处方子prescription_item", businessType = BusinessType.DELETE)
    @DeleteMapping("/{itemIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] itemIds) {
        return toAjax(iPrescriptionItemService.deleteWithValidByIds(Arrays.asList(itemIds), true) ? 1 : 0);
    }
}
